package com.example.studentapp.guide;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.studentapp.R;
import com.example.studentapp.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class GuideActivity extends AppCompatActivity {
    ViewPager viewPager;
    List<View> list;
    int[] pics;
    MyGuideAdapter myGuideAdapter;
    ImageView point1IV,point2IV,point3IV,point4IV,point5IV;
    ImageView[] points;
    // 这里解决首次打开app 不再进入引导页
    SharedPreferences sp;
    // 提取语言设置
    String lan_select;
    Resources resources;
    Configuration configuration;
    DisplayMetrics displayMetrics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        viewPager = findViewById(R.id.viewPager);
        point1IV = findViewById(R.id.point1IV);
        point2IV = findViewById(R.id.point2IV);
        point3IV = findViewById(R.id.point3IV);
        point4IV = findViewById(R.id.point4IV);
        point5IV = findViewById(R.id.point5IV);
        points = new ImageView[]{point1IV,point2IV,point3IV,point4IV,point5IV};
        resources = getResources();
        configuration = resources.getConfiguration();
        displayMetrics = resources.getDisplayMetrics();
        sp = getSharedPreferences("stuass", Context.MODE_PRIVATE);
        // 设置语言
        lan_select =sp.getString("lan",getString(R.string.login_simple));
        if(lan_select.equals(getString(R.string.login_simple))){
            configuration.locale = Locale.SIMPLIFIED_CHINESE;
        }else if(lan_select.equals(getString(R.string.login_en))){
            configuration.locale = Locale.ENGLISH;
        } else {
            configuration.locale = Locale.TAIWAN;
        }
        // 如果访问过就直接跳转到登陆页
        if(sp.getBoolean("guide",false)){
            Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();

        }
        list = new ArrayList<View>();
        pics = new int[]{R.drawable.guide,R.drawable.guide1,R.drawable.guide2,R.drawable.guide,R.drawable.guide3};
        for(int i =0;i<pics.length;i++){
            ImageView imageView = new ImageView(this);
            //imageView.setImageResource(pics[i]);
            imageView.setBackgroundResource(pics[i]);
            // 最后一张图片的监听事件 跳转到登陆页
            if(i == pics.length -1){
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                        // 写入标识
                        sp.edit().putBoolean("guide",true).commit();
                        startActivity(intent);
                        finish();
                    }
                });

            }
            list.add(imageView);
        }
        myGuideAdapter = new MyGuideAdapter(list);
        viewPager.setAdapter(myGuideAdapter);
        // 监听器
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            // 滑动到那一页
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        setPoint(0);
                        break;
                    case 1:
                        setPoint(1);
                        break;
                    case 2:
                        setPoint(2);
                        break;
                    case 3:
                        setPoint(3);
                        break;
                    case 4:
                        setPoint(4);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    void setPoint(int index){
        for(int i =0;i<points.length;i++){
            if(i==index){
                points[i].setImageResource(R.drawable.point_light);
            }else{
                points[i].setImageResource(R.drawable.point);
            }

        }
    }

    // 适配器
    class MyGuideAdapter extends PagerAdapter {
        List<View> list;
        MyGuideAdapter(List<View> list){
            this.list = list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
            //return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
            //super.destroyItem(container, position, object);
        }
    }
}